package cn.tedu.luban.worker.main.repository;

import cn.tedu.luban.worker.main.converter.WorkerAreaConverter;
import cn.tedu.luban.worker.main.mapper.WorkerAreaMapper;
import cn.tedu.luban.worker.protocol.bo.WorkerAreaBO;
import cn.tedu.luban.worker.protocol.dos.WorkerAreaDO;
import cn.tedu.luban.worker.protocol.param.WorkerRegistParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class WorkerAreaRepoImpl implements WorkerAreaRepository{

    @Autowired
    private WorkerAreaMapper workerAreaMapper;

    @Override
    public void delete(Long userId) {
        // delete form worker_area where user_id=#{userId}
        workerAreaMapper.deleteByUserId(userId);
    }

    @Autowired
    private WorkerAreaConverter workerAreaConverter;
    @Override
    public void saveWorkerAreas(WorkerRegistParam param) {

        // 对象转换
        List<WorkerAreaDO> dos =
        workerAreaConverter.params2dos(
                param.getWorkerRegistAreaParams(),param.getUserId());

        // 数据插入
        workerAreaMapper.batchInsert(dos);
    }

    @Override
    public List<WorkerAreaBO> findAreas(Long userId) {
        List<WorkerAreaDO> dos =  workerAreaMapper.selectByUserId(userId);
        return workerAreaConverter.dos2bos(dos);
    }

}





